package com.muqi.app.pj.shops;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.NetWorkUtils;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.MenuActivity;
import com.muqi.app.qlearn.student.R;

public class BuyXuniGoodActivity extends BaseFragmentActivity implements
		OnClickListener {
	public static final String GOODS_DETAILS_BEAN = "goods_fictitious_bean";

	private TextView goodsName;
	private TextView unitPrice;
	private ImageView goodsImg;
	private TextView goodsDes;
	private TextView totalPrice;
	private Button upThisBtn;

	private int quantity = 1;
	private TextView quantityTv;
	private ImageButton minusBtn;
	private ImageButton addBtn;
	private float totalMoney = 0;
	private float utPrice = 0;
	private FictitiousShopInfo getData;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getData = (FictitiousShopInfo) getIntent().getSerializableExtra(
				GOODS_DETAILS_BEAN);
		if (getData == null) {
			finish();
			return;
		}
		setContentView(R.layout.aty_buy_xuni_shop);
	}

	@Override
	protected void onInit() {

		goodsImg = (ImageView) findViewById(R.id.goods_icon);
		goodsName = (TextView) findViewById(R.id.goods_name);
		unitPrice = (TextView) findViewById(R.id.goods_price);
		goodsDes = (TextView) findViewById(R.id.goods_des);

		totalPrice = (TextView) findViewById(R.id.goods_total_price);
		upThisBtn = (Button) findViewById(R.id.pay_now_button);
		upThisBtn.setOnClickListener(this);
		
		minusBtn = (ImageButton)findViewById(R.id.goods_minus_btn);
		minusBtn.setOnClickListener(this);
		addBtn = (ImageButton)findViewById(R.id.goods_add_btn);
		addBtn.setOnClickListener(this);
		quantityTv = (TextView)findViewById(R.id.goods_number_tv);
	}

	@Override
	protected void onData() {
		utPrice=Float.parseFloat(getData.credits);
		goodsName.setText(getData.prizeInfo.title);
		unitPrice.setText(getData.credits + "积分");
		goodsDes.setText(getData.teacherInfo.name);
		if (getData.prizeInfo.fileurl != null) {
			Glide.with(this).load(getData.prizeInfo.fileurl.fileurl_02)
					.dontAnimate().placeholder(R.drawable.app_logo)
					.into(goodsImg);
		}
		setGoodsPriceView();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.pay_now_button:

			loadingGoodsOrder();
			upThisBtn.setEnabled(false);
			upThisBtn.setBackgroundResource(R.drawable.corners_grey_bg);

			break;
		case R.id.goods_minus_btn:
			if(quantity > 1){
				quantity = quantity - 1;
				setGoodsPriceView();
			}
			break;
		case R.id.goods_add_btn:
			if(quantity <getData.num){
				quantity = quantity + 1;
				setGoodsPriceView();
			}else{
				ShowToast.showShort(this, "已经没那么多库存了哦");
			}
			break;
		default:
			break;
		}
	}

	private void setGoodsPriceView() {
		//totalPrice.setText(getData.credits + "积分");
		//
		quantityTv.setText(quantity+"");
		totalPrice.setText(quantity*utPrice+"积分");
		totalMoney = quantity*utPrice;
	}

	private void loadingGoodsOrder() {
		showLoading();
		if (NetWorkUtils.isNetworkAvailable(this)) {
			
			HashMap<String, Object> sendMap = new HashMap<String, Object>();
			sendMap.put("token", mSpUtil.getToken());
			sendMap.put("shangpin_id", getData.id);
			sendMap.put("dizhi_id", "0");
			sendMap.put("zhifulaiyuan", "credit");
			sendMap.put("danjia", getData.credits);
			sendMap.put("shuliang", quantity);
			sendMap.put("zongjia", totalMoney);
			sendMap.put("yunfei", "0");
			sendMap.put("zongji", totalMoney);
			sendMap.put("liuyan", "");
			sendMap.put("taitou", "");
			sendMap.put("type", "xuni");
			sendMap.put("student_id", mSpUtil.getCurrentChildId());
			String requestUrl = ParamsUtils.buildParams(
					NetWorkApi.ORDER_WITH_SCORE_GOODS_API, sendMap);
			MyAsyncHttp.get(this, requestUrl, new AsyncCallBackListener() {

				@Override
				public void onAsyncCallBack(RESULTCODE resuleCode,
						String request, String result) {
					//Log.e("===", result);
					hideLoading();
					if (resuleCode == RESULTCODE.SUCCESS) {
			
						OrderIdInfo orderId=ResponseUtils.getOrderId(mContext, result);
						if(orderId!=null){
			
							Intent intent=new Intent(BuyXuniGoodActivity.this,OrderIdActivity.class);
							intent.putExtra("orderId", orderId.dingdanhao);
							intent.putExtra("payMoney", orderId.zongji);
							startActivity(intent);
						}
						
						
						
					}
				}
			});
		} else {
			ShowToast.showShort(this, R.string.net_break);// 提示无网络
		}
	}

	public void onBack(View v) {
		this.finish();
	}

	
}
